// The fileManager object
var fileManager = {
	tab_id: 0,
	init: function(){
		// Create a temporary file
		this.newFile();
	},
	newFile: function(_fileName){
		if(_fileName==undefined)
			_fileName = "Untitled";
	
		var id = "tabby_"+this.tab_id;
		this.tab_id+=1;
		
		var name = _fileName;
		var newTab = tabPanel.add({
            title: name,
            iconCls: 'tabs',
            html: '<textarea id="'+id+'"></textarea>',
            closable:true,
			fileName: name
        });
		
		newTab.show();
		$('#'+id).tabby();
		
		return newTab;
	},
	
	openFile: function(fileName){
		// Make sure it's not already open
			//TODO: Write this part
	
		var data = {title: fileName};
		$.post("/load/", data,
			function(data){
				var tab = fileManager.newFile(fileName);
				var textarea = tab.body.dom.childNodes[0];	
				textarea.value = data.content;
		}, "json");			
	},
	
	getCurrentFile: function(){
		var tab = tabPanel.getActiveTab();
		return tab.title;
	},
	
	
	
	saveCurrentFile: function(){
		var tab = tabPanel.getActiveTab();
		var textarea = tab.body.dom.childNodes[0];
	
		function promptCB(btn, text){
			if(btn!="ok")
				return; // do nothing if cancelled
				
			var data = {title: text,content: textarea.value};
			$.post("/save/", data,
				function(data){
					//saving = false;
					//$("#message").html('<span style="color:green">saved</span>');
			}, "json");
			
			// Update tab title
			tab.setTitle(text);
			//alert("btn:"+btn+"text:"+text);//Ext.example.msg('Button Click', 'You clicked the {0} button and entered the text "{1}".', btn, text);
		};
	
		Ext.MessageBox.prompt('Save File', 'Please enter file name:', promptCB, null, false, tab.title);
	}
};

function file(tab){
}

Ext.onReady(function(){
	fileManager.init();
		
	/*Ext.get("hideit").on('click', function() {
	   var w = Ext.getCmp('west-panel');
	   w.collapsed ? w.expand() : w.collapse(); 
	});*/
});